package com.huping.service.impl;

import ch.qos.logback.classic.Logger;
import com.alibaba.fastjson.JSONObject;
import com.huping.mapper.StudentMapper;
import com.huping.mapper.TeacherMapper;
import com.huping.mapper.UserMapper;
import com.huping.pojo.*;
import com.huping.service.BasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

@Service
public class BasicServiceImpl implements BasicService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;

    public User login(User user) {
        return userMapper.getByUsernameAndPassword(user);
    }

    public Result register(JSONObject param) {

        if (userMapper.getByUserid(param.getString("userid")) >= 1) return Result.error("用户名已存在");

        int userResult = userMapper.userInsert(
                new User(
                        null,
                        param.getString("userid"),
                        param.getString("password"),
                        param.getString("kind")));
        int kindResult;
        if (param.getString("kind").equals("1")) {
            kindResult = studentMapper.studentInsert(
                    new Student(
                            null,
                            param.getString("userid"),
                            param.getString("name"),
                            param.getString("gender"),
                            param.getString("phone"),
                            param.getString("email")));
        } else {
            kindResult = teacherMapper.teacherInsert(
                    new Teacher(
                            null,
                            param.getString("userid"),
                            param.getString("name"),
                            param.getString("gender"),
                            param.getString("phone"),
                            param.getString("email")));
        }


        if (kindResult != 0 && userResult != 0) return Result.success();
        return Result.error("插入失败");
    }
    public  Result getInformation(String userid,String kind){
        Student student;
        Teacher teacher;

    if (kind.equals("1")){

    student= studentMapper.getStudentInformationByUserid(userid);
        UserInformation userInformation=new UserInformation(
                student.getId(),
                student.getStudentId(),
                student.getName(),
                student.getGender(),
                student.getPhone(),
                student.getEmail()
        );
    return Result.success(userInformation);

    }
         else {
        teacher= teacherMapper.getTeacherInformationByUserid(userid);
        UserInformation userInformation=new UserInformation(
                teacher.getId(),
                teacher.getTeacherId(),
                teacher.getName(),
                teacher.getGender(),
                teacher.getPhone(),
                teacher.getEmail()
        );
        return Result.success(userInformation);
    }

    }

    public Result modification(JSONObject param,String userid,String kind) {



        int result2;//修改基本信息
        if (kind.equals("1")) {
            result2 = studentMapper.updateStudentInformation(new Student(
                    null,
                    userid,
                    param.getString("name"),
                    param.getString("gender"),
                    param.getString("phone"),
                    param.getString("email")));
        } else {
            result2 = teacherMapper.updateTeacherInformation(new Teacher(
                    null,
                    userid,
                    param.getString("name"),
                    param.getString("gender"),
                    param.getString("phone"),
                    param.getString("email")));

        }


        if(result2!=0)
          return Result.success("修改基本信息成功");
    else return Result.error("修改信息失败");


    }

    public  Result modifyPassword(JSONObject param,String userid){


        if(!param.getString("originPassword").equals(userMapper.getUserPassword(userid))){
            return Result.error("原密码错误");


        }

        userMapper.updateUserPassword(new User(
                null,
                userid,
                param.getString("password"),
                null

        ));
        return Result.success("密码修改成功");
    }

}
